package org.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

// 服务实例类 有服务名称、IP、端口、最后心跳时间  心跳时间用于判断服务是否存活

@AllArgsConstructor
@NoArgsConstructor
@Data
public class ServiceInstance {
    private String serviceName;
    private String ip;
    private int port;
    private long lastHeartbeat;


    public String toString(){
        return "ServiceInstance{" +
                "serviceName='" + serviceName + '\'' +
                ", ip='" + ip + '\'' +
                ", port=" + port +
                ", lastHeartbeat=" + lastHeartbeat +
                '}';
    }

    // 只比较 serviceName、ip、port
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ServiceInstance)) return false;
        ServiceInstance that = (ServiceInstance) o;
        return port == that.port &&
                Objects.equals(serviceName, that.serviceName) &&
                Objects.equals(ip, that.ip);
    }

    @Override
    public int hashCode() {
        return Objects.hash(serviceName, ip, port);
    }


    //便捷返回url:  http://ip:port
    public String getUrl() {
        return "http://" + ip + ":" + port;
    }
}
